package nl.jjsoft.airlock.utils
{
	import com.adobe.air.crypto.EncryptionKeyGenerator;
	
	import flash.utils.ByteArray;
	
	import nl.jjsoft.airlock.vo.PasswordPolicyVo;
	
	public class PasswordGenerator
	{
		// see: 
		//		http://www.adobe.com/devnet/air/flex/quickstart/encrypted_database.html		
		private static var keyGenerator:EncryptionKeyGenerator = new EncryptionKeyGenerator();
		
		private static const SALT:String = "ABBJCJDS88@828WDHJShdKJH8S98282HWJHDKJES";
		
		private static const LOWERCASE_CHARS:String = "abcdefghijklmnopqrstuvwxyz";
		private static const UPPERCASE_CHARS:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		private static const DIGITS:String = "0123456789";
		private static const SYMBOLS:String = "#@!$%&*()_-"; 
		
		public function PasswordGenerator()
		{
		}

		/**
		 * Generates and returns a password.
		 * @param strHash 
		 */
		public static function generatePassword(passwordPolicy:PasswordPolicyVo):String {
			var validCharacters:String = "";
			if (passwordPolicy.useDigits) {
				validCharacters += DIGITS;
			}
			if (passwordPolicy.useLowerCase) {
				validCharacters += LOWERCASE_CHARS;
			}
			if (passwordPolicy.useSymbols) {
				validCharacters += SYMBOLS;
			}
			if (passwordPolicy.useUpperCase) {
				validCharacters += UPPERCASE_CHARS;
			}
    		var password:String = "";
			
			for (var i:int = 0; i < passwordPolicy.length; i++) {
				password += validCharacters.charAt(Math.round(Math.random()*validCharacters.length));
			}    
			
			return password;
		}
		
		public static function getEncryptionKey(safeCombination:String):ByteArray {
			return keyGenerator.getEncryptionKey(safeCombination, SALT);
		}
		
		public static function isKeySecure(safeCombination:String):Boolean {
			return keyGenerator.validateStrongPassword(safeCombination);
		}
	}
}